#include "qlinewidget.h"

#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qhostfactory.h"

#include <QPainter>
#include <QGradient>

QLineWidget::QLineWidget(QWidget *parent) :
    QAbstractGraphicsWidget(parent)
{
}

void QLineWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing,m_antialiasing);
    p.setPen(m_pen);

    QPoint s,e;
    int off=m_pen_info.m_width/1.4;
    if(m_direction==LD_Left_Right)
    {
        s=QPoint(0,height()/2);
        e=QPoint(width(),height()/2);
    }
    else if(m_direction==LD_Top_Bottom)
    {
        s=QPoint(width()/2,0);
        e=QPoint(width()/2,height());
    }
    else if(m_direction==LD_LeftTop_RightBottom)
    {
        s=QPoint(off,off);
        e=QPoint(width()-off,height()-off);
    }
    else if(m_direction==LD_LeftBottom_RightTop)
    {
        s=QPoint(off,height()-off);
        e=QPoint(width()-off,off);
    }
    else
    {
        s=QPoint(off,height()/2);
        e=QPoint(width()-off,height()/2);
    }

    p.drawLine(s,e);
}

int QLineWidget::direction()
{
    return m_direction;
}

void QLineWidget::setDirection(int direction)
{
    if(m_direction!=direction)
    {
        m_direction=direction;
        update();
    }
}

QLineWidgetHost::QLineWidgetHost(QAbstractHost *parent):
    QAbstractGraphicsHost(parent)
{
}

void QLineWidgetHost::create_object()
{
    m_object=new QLineWidget;
    m_object->setObjectName("lineWidget");
}

QString QLineWidgetHost::get_show_name()
{
    return tr("Line");
}

QString QLineWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QLineWidgetHost::get_show_icon()
{
    return ":/base_grahpics_images/line.png";
}

void QLineWidgetHost::init_property()
{
    QAbstractGraphicsHost::init_property();

    QAbstractProperty* pro;

    pro=get_property("pen");
    if(pro!=NULL)
    {
        pro->set_attribute("pen_join_style:Visible",false);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","direction");
        pro->set_attribute("show_name",tr("Direction"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        ComboItems items;
        tagComboItem item;
        item.m_text="Left-Right";
        item.m_value=LD_Left_Right;
        items.append(item);
        item.m_text="Top-Bottom";
        item.m_value=LD_Top_Bottom;
        items.append(item);
        item.m_text="LeftTop-RightBottom";
        item.m_value=LD_LeftTop_RightBottom;
        items.append(item);
        item.m_text="LeftBottom-RightTop";
        item.m_value=LD_LeftBottom_RightTop;
        items.append(item);
        pro->set_attribute("items",QVariant::fromValue<ComboItems>(items));
        m_object->setProperty("direction",LD_Left_Right);
        insert_property(pro);
    }


    set_property_value("geometry",QRect(0,0,100,10));
}

void QLineWidgetHost::setDirection(int direction)
{
    set_property_value("direction",direction);
}

int QLineWidgetHost::direction()
{
    return get_property_value("direction").toInt();
}

qRegisterHost("basegraphics.line",QLineWidgetHost,5000)
